package ie.three.webtexts;

import ie.three.webtexts.base.WebtextBaseActivity;
import ie.three.webtexts.contacts.WebtextsContacts;

import java.text.MessageFormat;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class WebtextSendActivity extends WebtextBaseActivity {

	public Button sendMessageBtn;
	public Button pickContactBtn;
	public TextView remainingTexts;
	public EditText contacts;
	public LinearLayout l;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Intent i = getIntent();
		Boolean showLoginSuccess = (Boolean) i.getExtras().get(
				"showLoginSuccess");

		// final Context ctx = getApplicationContext();
		if (showLoginSuccess) {
			CharSequence txt = getString(R.string.login_success);
			int duration = Toast.LENGTH_SHORT;
			Toast toast = Toast.makeText(this, txt, duration);
			toast.show();
		}
		
		setContentView(R.layout.send);

		pickContactBtn = (Button) findViewById(R.id.get_contacts);
		pickContactBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				contacts = (EditText) findViewById(R.id.recipients);
				String s = "";
				if (null != contacts.getText()) {
					s = contacts.getText().toString();
				}
				pickContact(s);
				// sendMessage();
			}
		});

		sendMessageBtn = (Button) findViewById(R.id.send_message);
		sendMessageBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// pickContact();
				sendMessage();
			}
		});

		remainingTexts = (TextView) findViewById(R.id.remaining_texts);
		remainingTexts.setText(MessageFormat.format(
				getString(R.string.remaining_texts),
				String.valueOf(i.getExtras().getInt("remainingAllowance"))));

	}

	public void sendMessage() {
		Log.i(TAG, "Send message clicked");

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.send_message_menu, menu);
		return true;
	}

	private void pickContact(String contacts) {
		// Create an intent to "pick" a contact, as defined by the content
		// provider URI
		Intent intent = new Intent(Intent.ACTION_PICK,
				ContactsContract.Contacts.CONTENT_URI);
		intent.putExtra("contacts", contacts);
		startActivityForResult(intent, PICK_CONTACT_REQUEST);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case PICK_CONTACT_REQUEST:
				final EditText phoneInput = (EditText) findViewById(R.id.recipients);
				phoneInput.setText(new WebtextsContacts()
						.getContact(data, this));
				break;
			}
		} else {
			// activity result error actions
		}
	}

}
